package com.hamster.tools.i18nManger.model
{
	import com.hamster.tools.i18nManger.events.AppEvent;
	import com.hamster.tools.i18nManger.vo.ConfigurationVO;
	import com.hamster.tools.i18nManger.vo.PropertyFileGroupVO;
	import com.hamster.tools.i18nManger.vo.PropertyFileVO;
	import com.hamster.tools.i18nManger.vo.PropertyItemVO;
	
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	import mx.collections.ArrayCollection;
	import mx.utils.StringUtil;
	
	import org.puremvc.as3.patterns.proxy.Proxy;
	
	public class PropertyFileVOProxy extends Proxy
	{
		public static const NAME:String = "PropertyFileVOProxy";
		
		public var fileGroupVOList:ArrayCollection;
		
		
		public function PropertyFileVOProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		public function scan(folder:File):void
		{
			fileGroupVOList = new ArrayCollection();
			scanPropertyFiles(folder);
			
			var fileGroupVOResult:ArrayCollection = new ArrayCollection();
			if (configurationVO.filterCompletedFolder == "Y") {
				var targetLang:String = "zh_CN";
				for each (var group:PropertyFileGroupVO in fileGroupVOList) {
					var targetFile:PropertyFileVO;
					for each (var file:PropertyFileVO in group.fileList) {
						if (file.language == "zh_CN") {
							targetFile = file;
							break;
						}
					}
					if (targetFile == null) {
						fileGroupVOResult.addItem(group);
					} else {
						var exists:Boolean;
						for each (file in group.fileList) {
							if (file.language != "zh_CN") {
								for each (var item:PropertyItemVO in file.itemList) {
									exists = false;
									for each (var targetItem:PropertyItemVO in targetFile.itemList) {
										if (targetItem.key == item.key) {
											exists = true;
											break;
										}
									}
									if (!exists) {
										fileGroupVOResult.addItem(group);
										break;
									}
								}
								if (!exists) {
									break;
								}
							}
							
						}
					}
				}
			}
			
			sendNotification(AppEvent.SCAN_TARGET_FOLDER_RESPONSE, fileGroupVOResult);
		}
		
		public function scanPropertyFiles(folder:File):void
		{
			var fileList:Array = folder.getDirectoryListing();
			
			var fileGroupVO:PropertyFileGroupVO = new PropertyFileGroupVO();
			fileGroupVO.folderPath = folder.nativePath;
			fileGroupVO.fileList = new ArrayCollection();
			fileGroupVO.languageList = new ArrayCollection();
			
			for each (var subFile:File in fileList) {
				if (!subFile.isDirectory) {
					if (folder.name == "_i18n" && subFile.name.indexOf("LocalStrings") == 0) {
						trace ("Catch File : " + subFile.nativePath);
						var language:String = subFile.name.replace(/LocalStrings_/g, "")
							.replace(/.properties/g, "");
						var propertyFileVO:PropertyFileVO = new PropertyFileVO();
						propertyFileVO.fileName = subFile.name;
						propertyFileVO.filePath = subFile.nativePath;
						propertyFileVO.language = language;
						propertyFileVO.parentGroup = fileGroupVO;
						propertyFileVO.itemList = buildItemVOList(subFile, propertyFileVO);
						fileGroupVO.fileList.addItem(propertyFileVO);
						fileGroupVO.languageList.addItem(language);
					}
				} else if (subFile.isDirectory) {
					scanPropertyFiles(subFile);
				}
			}
			if (fileGroupVO.fileList.length > 0) {
				fileGroupVOList.addItem(fileGroupVO);
			}
		}
		
		public function buildItemVOList(file:File, parentFileVO:PropertyFileVO):ArrayCollection
		{
			var stream:FileStream = new FileStream();
			stream.open(file, FileMode.READ);
			var fileString:String = stream.readUTFBytes(file.size);
			var fileLineList:Array;
			var fileSeparator:String = '\n';
			if (fileString.indexOf('\r\n') >= 0) {
				fileString = fileString.replace(/\r\n/g, '\n');
			}
			fileLineList = fileString.split(fileSeparator);
			
			var resultArr:ArrayCollection = new ArrayCollection();
			var itemVO:PropertyItemVO;
			for each (var fileLineStr:String in fileLineList) {
				if (fileLineStr.indexOf("#") == 0) {
					continue;
				} else if (fileLineStr.indexOf('=') >= 0) {
					itemVO 			= new PropertyItemVO();
					itemVO.key 		= StringUtil.trim(fileLineStr.substring(0, fileLineStr.indexOf('=')));
					itemVO.value 	= StringUtil.trim(fileLineStr.substring(fileLineStr.indexOf('=') + 1));
					itemVO.value 	= unescape(itemVO.value.replace(/\\\u/g, '%u'));
					itemVO.parentFile = parentFileVO;
					itemVO.language = parentFileVO.language;
					resultArr.addItem(itemVO);
				} else if (itemVO != null && StringUtil.trim(fileLineStr).length > 0) {
					itemVO.value += fileSeparator + fileLineStr;
				}
			}
			
			return resultArr;
		}
		
		public function get configurationVO():ConfigurationVO
		{
			return (facade.retrieveProxy(ConfigurationVOProxy.NAME) as ConfigurationVOProxy).configurationVO;
		}
	}
}